jQuery.validator.addMethod("valueSelection", function (value, element) {
    return this.optional(element) || (parseFloat(value) != -1);
}, "Required");

jQuery.validator.addMethod("armspass", function (value, element) {
    return this.optional(element) || /^.*(?=.{8,})(((?=.*[a-z])(?=.*[A-Z])(?=.*[\d]))|((?=.*[a-z])(?=.*[A-Z])(?=.*[\W]))|((?=.*[a-z])(?=.*[\d])(?=.*[\W]))|((?=.*[A-Z])(?=.*[\d])(?=.*[\W]))).*$/.test(value);
}, "Invalid combination or length");

jQuery.validator.addMethod("inventoryQuantity", function (value, element) {
    return ($.trim(value).length == 0) || (/^[0-9]\d{0,7}(\.\d{1,2})?$/.test(value) && (parseFloat(value) > 0));
}, "Invalid Quantity");

jQuery.validator.addMethod("inventoryAmount", function (value, element) {
    return ($.trim(value).length == 0) ||  (/^[0-9]\d{0,7}(\.\d{1,2})?$/.test(value) && (parseFloat(value) > 0));
}, "Invalid Amount");

jQuery.validator.addMethod("misWholeNumber", function (value, element) {
    return ($.trim(value).length == 0) || ((/^[0-9]\d{0,7}?$/.test(value)) && (parseInt(value) > 0));
}, "Invalid Number");


function showError(errDescrip) {
    if (errDescrip.length == 0) {
        return false;
    }
    $.jGrowl(errDescrip, 'error');
}

function showConstraintsErrors(errors) {


    // Growl massages should display in <Li> tag in a single growl message
    var erroHtml = '<ul>';
    errors.each(function (i) {
        var err = $(this);
        var errStr = 'Error(s) occurred in some inputs';
        try {
            if (err.length == 2) {
                if ($("label[for='" + err[0] + "']").html() != null) {  // get the label of that field
                    errStr = $("label[for='" + err[0] + "']").html() + ' ' + err[1];
                }
            } else if (err.length == 1) {
                errStr = err[0]
            }
        } catch (e) {
        }
        erroHtml += '<li>' + errStr + '</li>';
    });
    erroHtml = erroHtml + '</ul>';
    showError(erroHtml);
}

function clearDivErrors() {
    $('#errorDiv').empty();
    $('#errorDiv').hide();
}

function showSuccess(successDescrip) {
    if (successDescrip.length == 0) {
        return false;
    }

    // new imp
    $.jGrowl(successDescrip, 'success');
}

function showInfo(errDescrip) {
    if (!errDescrip || errDescrip.length == 0) {
        return false;
    }
    $.jGrowl(errDescrip, 'info');
}

String.prototype.endsWith = function (str) {
    return (this.match(str + "$") == str)
}

String.prototype.isEmpty = function () {
    try {
        return ($.trim(this).length == 0);
    } catch (e) {
        return true;
    }
}


function onCompleteAjaxCall(XMLHttpRequest, textStatus) {
    showLoadingSpinner(false);
}


$.fn.refreshDropDown = function (data, options) {

    try {
        var ctrlDropDown = this;
        ctrlDropDown.children().remove();

        options = $.extend({
            valueMember:'id',
            textMember:'name',
            addAllAttributes:false,
            unselectedValue:'-1',
            selectHints:'Please Select...',
            defaultValue:-1
        }, options);


        if (options.unselectedValue) {
            $($('<option></option>')).val(options.unselectedValue).html(options.selectHints).appendTo(ctrlDropDown);
        }

        $(data).each(function (e) {
            var opt = $($('<option></option>'));
            $.each(this, function (k, v) {
                if (k == options.textMember) opt.html(v);
                else if (k == options.valueMember) opt.val(v);
                else if (options.addAllAttributes) opt.attr(k, v);
            });
            opt.appendTo(ctrlDropDown);
        });

        // initial selection with default
        if (options.defaultValue) {
            ctrlDropDown.attr('value', options.defaultValue);
        }
    } catch (e) {

    }

};

function getGridHeight(numberOfGridBar) {
    var extraHieght = 0;
    if ($('#message_div').length) {
        extraHieght = $('#message_div').height() + 4;
    }
    var totalHeight = (27 * 2 + 31 + 8 + 2);
    if (numberOfGridBar != 3) {
        totalHeight += 27;
    }

    gridHeight = $("#contentHolder").height() - ($("#application_top_panel").height() + 2 + totalHeight);
    return gridHeight;
}
function getFullGridHeight() {
    var totalHeight = (27 * 2 + 31 + 8 + 2) + 5;
    if ($('.tDiv').length) {
        totalHeight += 27;
    }
    gridHeight = $("#contentHolder").height() - totalHeight;
    return gridHeight;
}

function isEmpty(val) {
    var val2;
    val2 = $.trim(val);
    return (val2.length == 0);
}
